"""
Case Type   : 系统内部使用工具
Case Name   : 指定多个目录包含到备份中，执行备份
Create At   : 2021/03/04
Owner       : opentestcase015
Description :
    1.新建目录
    2.进行初始化
    3.在备份路径内初始化一个新的备份实例
    4.执行备份
    5.删除新建目录
Expect      :
    1.新建目录成功
    2.进行初始化成功
    3.在备份路径内初始化一个新的备份实例成功
    4.执行备份成功，有重定向的外部文件
    5.删除新建目录
History     :
    modified by l1azzzy at 2024/12/5:优化提示信息，以及逻辑，适配7.0.0-RC1版本
"""

import unittest
import re

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from yat.test import Node
from yat.test import macro

LOG = Logger()


class SystemInternalTools(unittest.TestCase):
    def setUp(self):
        LOG.info('-------------------this is setup--------------------')
        LOG.info('-Opengauss_Function_Tools_Gs_Probackup_Case0059开始执行-')
        self.constant = Constant()
        self.PrimaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')

    def test_system_internal_tools(self):
        LOG.info('---------step1 新建文件与备份目录--------------')
        instance_path = f'{macro.DB_INSTANCE_PATH}'
        LOG.info('实例路径为：' + instance_path)
        index1 = instance_path.find('/')
        index2 = instance_path.rfind('/')
        self.cluster_path = instance_path[index1:index2]
        LOG.info(self.cluster_path)
        init_cmd = f"mkdir {self.cluster_path}/testdir;" \
            f"mkdir {self.cluster_path}/extdir1;" \
            f"mkdir {self.cluster_path}/extdir2;" \
            f"touch {self.cluster_path}/extdir1/aaa;" \
            f"touch {self.cluster_path}/extdir2/bbb;" \
            f"echo 'gs_probackup test'>>{self.cluster_path}/extdir1/aaa;" \
            f"echo 'gs_probackup test'>>{self.cluster_path}/extdir2/bbb;" \
            f"ls {self.cluster_path}/extdir1;" \
            f"ls {self.cluster_path}/extdir2;"
        LOG.info(init_cmd)
        init_msg = self.PrimaryNode.sh(init_cmd).result()
        LOG.info(init_msg)
        self.assertIn('aaa', init_msg)
        self.assertIn('bbb', init_msg)

        LOG.info('----------step2 进行初始化------------------')
        init_cmd = f"source {macro.DB_ENV_PATH};gs_probackup init -B " \
            f"{self.cluster_path}/testdir;"
        LOG.info(init_cmd)
        init_msg = self.PrimaryNode.sh(init_cmd).result()
        LOG.info(init_msg)
        self.assertIn(self.constant.init_success, init_msg)

        LOG.info('-----step3 在备份路径内初始化一个新的备份实例---')
        init_cmd = f"source {macro.DB_ENV_PATH};" \
            f"gs_probackup add-instance -B {self.cluster_path}/testdir " \
            f"-D {macro.DB_INSTANCE_PATH} --instance=pro1;"
        LOG.info(init_cmd)
        init_msg = self.PrimaryNode.sh(init_cmd).result()
        LOG.info(init_msg)
        self.assertIn("'pro1' " + self.constant.init_success, init_msg)

        LOG.info('-------------step4 执行全量备份---------------')
        back_cmd = f"source {macro.DB_ENV_PATH};" \
            f"gs_probackup backup -B {self.cluster_path}/testdir " \
            f" --instance=pro1 -b full  -d {self.PrimaryNode.db_name} -p " \
            f"{self.PrimaryNode.db_port} --compress-algorithm=pglz -E " \
            f"{self.cluster_path}/extdir1:{self.cluster_path}/extdir2 ;"
        LOG.info(back_cmd)
        back_msg = self.PrimaryNode.sh(back_cmd).result()
        LOG.info(back_msg)
        self.assertIn('completed', back_msg)
        self.backupmsg = re.findall("Backup .*? completed", back_msg)
        LOG.info(self.backupmsg)
        self.backupid = self.backupmsg[0].split()[1]
        LOG.info('备份ID为：' + self.backupid)
        check_cmd1 = f"ls " \
            f"{self.cluster_path}/testdir/backups/pro1/{self.backupid}" \
            f"/external_directories/externaldir1;"
        LOG.info(check_cmd1)
        check_msg1 = self.PrimaryNode.sh(check_cmd1).result()
        LOG.info(check_msg1)
        self.assertIn('aaa', check_msg1)
        check_cmd2 = f"ls " \
            f"{self.cluster_path}/testdir/backups/pro1/{self.backupid}" \
            f"/external_directories/externaldir2;"
        LOG.info(check_cmd2)
        check_msg2 = self.PrimaryNode.sh(check_cmd2).result()
        LOG.info(check_msg2)
        self.assertIn('bbb', check_msg2)

    def tearDown(self):
        LOG.info('------------------this is tearDown--------------------')
        LOG.info('------------------step5 删除新建目录------------------')
        clear_cmd = f"rm -rf {self.cluster_path}/testdir;" \
            f"rm -rf {self.cluster_path}/extdir1;" \
            f"rm -rf {self.cluster_path}/extdir2;"
        LOG.info(clear_cmd)
        clear_msg = self.PrimaryNode.sh(clear_cmd).result()
        LOG.info(clear_msg)
        LOG.info('-Opengauss_Function_Tools_Gs_Probackup_Case0059执行完成-')
